<?
/**
 * Copyright 2008 Melange.
 *
 * This file is part of Melange CMS
 *
 * @package cms
 *
 */


/**
 * Requirements
 */
require_once("mvc/action/ActionForm.php");
require_once("mvc/action/ActionMessage.php");
require_once("mvc/action/ActionMessages.php");
require_once("cms/request/om/ContactRequest.php");



/**
 * BaseRequestForm Class
 *
 * @category    Melange CMS
 * @package     cms
 * @copyright   Copyright (c) 2007 Jeroen Simons. All rights reserved
 * @author      Jeroen Simons <jeroen@melange.nl>
 * @link        http://www.melange.nl/
 */
class BaseRequestForm extends ActionForm {


    // ----------------------------------------------------- Instance Variables


    /**
     * request
     *
     * @var ContactRequest 
     */
    protected $request;


    // ----------------------------------------------------------- Constructors


    /**
     * Construct new BaseRequestForm
     */
    public function __construct() {
        $this->request = new ContactRequest();
    }


    // ------------------------------------------------------------- Properties


    /**
     * Get Request
     *
     * @return ContactRequest 
     */
    public function getRequest() {
        return $this->request;
    }


    /**
     * Set Request
     *
     * @param ContactRequest  request
     */
    public function setRequest(ContactRequest $request) {
        $this->request = $request;
    }


    // --------------------------------------------------------- Public Methods


    /**
     * Validate this form
     *
     * @return array $errors
     */
    public function validate() {

        $errors = new ActionMessages();

        if(is_null($this->getRequest()->getFirstname()) || strlen($this->getRequest()->getFirstname()) == 0)
            $errors->add("request->firstname",
                new ActionMessage("errors.required", true, new ActionMessage("prompt.firstname", true)));


        if(!is_null($this->getRequest()->getFirstname()) && strlen($this->getRequest()->getFirstname()) > 0 && strlen($this->getRequest()->getFirstname()) > 255)
            $errors->add("request->firstname",
                new ActionMessage("errors.maxlength", true, array(
                    new ActionMessage("prompt.firstname", true),
                    new ActionMessage("255", false))));


        if(is_null($this->getRequest()->getSurname()) || strlen($this->getRequest()->getSurname()) == 0)
            $errors->add("request->surname",
                new ActionMessage("errors.required", true, new ActionMessage("prompt.surname", true)));


        if(!is_null($this->getRequest()->getSurname()) && strlen($this->getRequest()->getSurname()) > 0 && strlen($this->getRequest()->getSurname()) > 255)
            $errors->add("request->surname",
                new ActionMessage("errors.maxlength", true, array(
                    new ActionMessage("prompt.surname", true),
                    new ActionMessage("255", false))));


        if(is_null($this->getRequest()->getEmail()) || strlen($this->getRequest()->getEmail()) == 0)
            $errors->add("request->email",
                new ActionMessage("errors.required", true, new ActionMessage("prompt.email", true)));


        if(!is_null($this->getRequest()->getEmail()) && strlen($this->getRequest()->getEmail()) > 0 && !ereg('^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$', $this->getRequest()->getEmail()))
            $errors->add("request->email",
                new ActionMessage("errors.email", true, new ActionMessage("prompt.email", true)));


        if(!is_null($this->getRequest()->getEmail()) && strlen($this->getRequest()->getEmail()) > 0 && strlen($this->getRequest()->getEmail()) > 255)
            $errors->add("request->email",
                new ActionMessage("errors.maxlength", true, array(
                    new ActionMessage("prompt.email", true),
                    new ActionMessage("255", false))));


        return $errors;
    }

}
?>